<template>
  <div class="login">
    <!-- 背景装饰元素 -->
    <div class="bg-circle circle-1"></div>
    <div class="bg-circle circle-2"></div>
    <!-- 登录卡片 -->
    <div class="login-container">
      <!-- 左侧欢迎区 -->
      <div class="welcome-section">
        <div>
          <div class="logo">
            <i class="fas fa-rocket"></i>
            <span>协同办公平台</span>
          </div>

          <div class="welcome-content">
            <h1>欢迎回来</h1>
            <p>登录您的账户，即刻开启高效协作之旅。我们为您提供全面的一体化办公解决方案。</p>
          </div>
        </div>

        <ul class="features">
          <li><i class="fas fa-check-circle floating" style="animation-delay: 0.2s"></i> 实时团队协作</li>
          <li><i class="fas fa-check-circle floating" style="animation-delay: 0.4s"></i> 文件共享与版本控制</li>
          <li><i class="fas fa-check-circle floating" style="animation-delay: 0.6s"></i> 项目进度跟踪</li>
        </ul>
      </div>

      <!-- 右侧登录表单 -->
      <div class="login-form">
        <div class="form-header">
          <h2>账户登录</h2>
          <p>请输入您的凭证以访问您的账户</p>
        </div>
        <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="" label-position="top">
          <el-form-item prop="loginName" class="form-group" label="账号">
            <el-input
                v-model="loginForm.loginName"
                type="text"
                size="large"
                auto-complete="off"
                placeholder="请输入账号"
            >
              <template #prefix><SvgIcon name="User" class="mr8"></SvgIcon></template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password" class="form-group" label="密码">
            <el-input
                v-model="loginForm.password"
                type="password"
                size="large"
                auto-complete="off"
                placeholder="请输入密码"
                @keyup.enter="handleLogin(loginRef)"
            >
              <template #prefix><SvgIcon name="Lock" class="mr8"></SvgIcon></template>
            </el-input>
          </el-form-item>
          <div class="options">
            <div class="remember-me">
              <input type="checkbox" id="remember">
              <label for="remember">记住我</label>
            </div>
            <div class="forgot-password">
              <a href="#">忘记密码?</a>
            </div>
          </div>

          <el-form-item style="width:100%;">
            <el-button
                class="btn"
                :loading="loading"
                size="large"
                type="primary"
                style="width:100%;"
                @click.prevent="handleLogin(loginRef)"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
            <div style="float: right;" v-if="register">
              <router-link class="link-type" :to="'/register'">立即注册</router-link>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2025  All Rights Reserved.</span>
    </div>
  </div>
</template>

<script setup name="Login">
import {onMounted, reactive, ref, toRefs , watch} from "vue";
import { useRoute , useRouter } from 'vue-router';
import  * as LoginApi from "@/api/login";
import {getCodeImg} from "@/api/login";
import {ElMessage} from "element-plus";
import { useAuthStore } from '@/stores/auth';
import { Local } from '@/utils/storage';


const authStore = useAuthStore();
// 获取当前路由对象
const route = useRoute();
//路由管理器
const router = useRouter();
const loginRef = ref('');

const loginForm = ref({
  loginName: "admin",
  password: "123456",
  rememberMe: false,
  systemType: 2,
  code: "",
  uuid: ""
});

const loginRules = {
  loginName: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }]
};

const codeUrl = ref("");
const loading = ref(false);
// 验证码开关
const captchaEnabled = ref(true);
// 注册开关
const register = ref(false);
const redirect = ref(undefined);

watch(route, (newRoute) => {
  console.log("登录--" , newRoute);
    redirect.value = newRoute.query && newRoute.query.redirect;
}, { immediate: true });

function handleLogin(formEl) {
  if (!formEl) return;
  formEl.validate(async valid => {
    if (valid) {
      loading.value = true;
      try {
        let res = await LoginApi.login(loginForm.value)
        if (res.code === 200) {
          // authStore.setUserInfo(res.data.userInfo);
          // authStore.setAuthCodeList(res.data.authCode)
          // authStore.setMenuList(res.data.menuList)
          Local.set("userInfo", res.data.userInfo);
          Local.setStr("token", res.data.token);
          // router.push({ path: redirect.value || "/" });
          router.replace({path: route.query?.redirect || '/'});
          ElMessage.success("登录成功");
        }
      }catch (e){
        //Promise.reject(response) 会被catch到
        loading.value = false;
      }
    }
  });
}

function getCode() {
  getCodeImg().then(res => {
    captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled;
    if (captchaEnabled.value) {
      codeUrl.value = "data:image/gif;base64," + res.img;
      loginForm.value.uuid = res.uuid;
    }
  });
}

function getCookie() {
}

//getCode();
//getCookie();

function createParticle() {
  const particle = document.createElement('div');
  particle.classList.add('bg-circle');
  // 随机大小
  const size = Math.random() * 100 + 50;
  particle.style.width = `${size}px`;
  particle.style.height = `${size}px`;
  // 随机位置
  particle.style.left = `${Math.random() * 100}%`;
  particle.style.top = `${Math.random() * 100}%`;
  // 随机透明度
  particle.style.opacity = Math.random() * 0.2 + 0.05;
  document.body.appendChild(particle);
  // 添加动画
  animateParticle(particle);
}

function animateParticle(element) {
  let x = parseInt(element.style.left) || 50;
  let y = parseInt(element.style.top) || 50;
  let xSpeed = (Math.random() - 0.5) * 0.2;
  let ySpeed = (Math.random() - 0.5) * 0.2;
  function move() {
    x += xSpeed;
    y += ySpeed;
    // 边界检查
    if(x < -20 || x > 100) xSpeed *= -1;
    if(y < -20 || y > 100) ySpeed *= -1;
    element.style.left = `${x}%`;
    element.style.top = `${y}%`;
    requestAnimationFrame(move);
  }
  move();
}

onMounted(() => {
  //createParticle();
});

</script>

<style lang='scss' scoped>

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 40px;
    input {
      height: 40px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 40px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 40px;
  padding-left: 12px;
}

.login {
  position: relative;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #333;
  overflow: hidden;
}

/* 背景装饰元素 */
.bg-circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
}

.circle-1 {
  width: 600px;
  height: 600px;
  right: -100px;
  top: -100px;
}

.circle-2 {
  width: 400px;
  height: 400px;
  left: -100px;
  bottom: -100px;
}

/* 登录卡片容器 */
.login-container {
  position: relative;
  backdrop-filter: blur(10px);
  width: 1000px;
  height: 600px;
  display: flex;
  z-index: 10;
  box-shadow: 0 25px 45px rgba(0, 0, 0, 0.2);
  border-radius: 15px;
  overflow: hidden;
}

/* 左侧欢迎区域 */
.welcome-section {
  width: 40%;
  background: rgba(255, 255, 255, 0.15);
  padding: 50px;
  color: white;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  position: relative;
  overflow: hidden;
}

.logo {
  display: flex;
  align-items: center;
  margin-bottom: 30px;
  font-size: 24px;
  font-weight: 700;
  z-index: 1;
}

.logo i {
  margin-right: 10px;
  font-size: 28px;
}

.welcome-content {
  z-index: 1;
}

.welcome-content h1 {
  font-size: 32px;
  margin-bottom: 20px;
  font-weight: 600;
}

.welcome-content p {
  font-size: 16px;
  line-height: 1.6;
  opacity: 0.9;
  margin-bottom: 30px;
}

.features {
  list-style: none;
  margin-top: auto;
}

.features li {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
  font-size: 14px;
}

.features i {
  margin-right: 10px;
  color: #fff;
}

/* 右侧登录表单 */
.login-form {
  width: 60%;
  background: white;
  padding: 60px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.form-header {
  margin-bottom: 40px;
}

.form-header h2 {
  font-size: 28px;
  color: #333;
  margin-bottom: 10px;
  font-weight: 600;
}

.form-header p {
  color: #666;
  font-size: 14px;
}

.form-group {
  margin-bottom: 25px;
  position: relative;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-size: 14px;
  color: #555;
  font-weight: 500;
}

.input-with-icon {
  position: relative;
}

.input-with-icon i {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  color: #888;
  font-size: 16px;
}

.form-control {
  width: 100%;
  padding: 12px 12px 12px 45px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 14px;
  transition: all 0.3s;
}

.form-control:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2);
}

.options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 25px;
  font-size: 14px;
}

.remember-me {
  display: flex;
  align-items: center;
}

.remember-me input {
  margin-right: 8px;
}

.forgot-password a {
  color: #667eea;
  text-decoration: none;
  transition: color 0.3s;
}

.forgot-password a:hover {
  color: #764ba2;
}

.btn {
  display: inline-block;
  background: linear-gradient(to right, #667eea, #764ba2);
  color: white;
  border: none;
  padding: 14px;
  width: 100%;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
  margin-bottom: 20px;
}

.btn:hover {
  background: linear-gradient(to right, #5a6fd6, #6a4098);
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(102, 126, 234, 0.3);
}

.social-icons {
  display: flex;
  justify-content: center;
  gap: 15px;
}

.social-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  cursor: pointer;
  transition: transform 0.3s;
}

.social-icon:hover {
  transform: translateY(-5px);
}

.facebook {
  background: #3b5998;
}

.google {
  background: #db4437;
}

.github {
  background: #333;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .login-container {
    width: 90%;
    height: auto;
  }
}

@media (max-width: 768px) {
  .login-container {
    flex-direction: column;
    height: auto;
  }

  .welcome-section, .login-form {
    width: 100%;
  }

  .welcome-section {
    padding: 30px;
  }

  .login-form {
    padding: 40px 30px;
  }

  .circle-1, .circle-2 {
    display: none;
  }
}

/* 动画效果 */
@keyframes float {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-10px);
  }
}

.floating {
  animation: float 3s ease-in-out infinite;
}
</style>
